import { defineStore } from 'pinia';
import { getLoginUserUsingGet } from '@/service/backend/userController';
import { ref } from 'vue';

const useUserStore = defineStore('user', () => {
  const loginUser = ref<API.LoginUserVO>({
    username: '未登录'
  });

  /**
   * 远程获取登录用户信息
   */
  async function fetchLoginUser() {
    const res = await getLoginUserUsingGet();
    // console.log(res);
    if (res.code === 0 && res.data) {
      loginUser.value = res.data;
    }
  }

  /**
   * 设置登录用户
   * @param newLoginUser
   */
  function setLoginUser(newLoginUser: any) {
    loginUser.value = newLoginUser;
  }

  /**
   * 重置登录用户
   */
  const resetLoginUser = () => {
    loginUser.value = {
      userName: '未登录'
    };
  };
  // 返回
  return { loginUser, fetchLoginUser, setLoginUser, resetLoginUser };
});

export default useUserStore;
